/**
 * @author : dormi330
 * @date : 2020-03-11
 * description : 文件描述
 */

package org.dormi.learn.promotion.util;


import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class PropertyLoader {

    /**
     * 加载 resouces/xxx.properties
     *
     * @param file 文件全名, 如 test1.properties
     * @return properties
     * @throws IOException
     */
    public static Properties getFromResouces(String file) throws IOException {
        Properties prop = new Properties();
        try (InputStream input = PropertyLoader.class.getClassLoader().getResourceAsStream(file)) {
            if (input == null) {
                System.out.println("Sorry, unable to find " + file);
                return null;
            }
            prop.load(input);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return prop;
    }

    public static void main(String[] args) throws IOException {
        String fileName = "test1.properties";
        Properties prop = getFromResouces(fileName);
        if (prop != null) {
            prop.forEach((k, v) -> System.out.println(k + "=" + v));
        } else {
            System.out.println("load fail " + fileName);
        }
    }
}
